// pages/cart/index.js
import {getSetting,chooseAddress,openSetting,showModel,showToast} from "../../utils/asyncWx.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    allChecked:false,
    totalPrice:0,
    totalNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

 async  handleChooseAddress(){
    try {
      //1 获取权限 状态
      const res1 = await getSetting();
      const scopeAddress = res1.authSetting["scope.address"];
      console.log(scopeAddress);
      //2 判断权限状态
      if(scopeAddress===false){
        //3 先诱导用户打开授权页面
        await openSetting();
      }

      //4 调用获去地址的api
      let address = await chooseAddress();
      address.all=address.provinceName+address.cityName+address.countyName+address.detailInfo;
      //5 存入发哦缓存中
      wx.setStorageSync("address", address);
    } catch (error) {
      console.log(error);
    }
  },

  //商品的选中
  handleItemChange(e){
    //修改的商品id
    const goods_id = e.currentTarget.dataset.id;
    //获取数组
    let {cart} =this.data;
    let index = cart.findIndex(v=>v.goods_id===goods_id);
    //选中状态取反
    cart[index].checked = !cart[index].checked;
   this.setCart(cart);
  },

  //设置购物车状态
  setCart(cart){
   
    let allChecked = true;
    let totalPrice = 0;
    let totalNum  = 0;
    cart.forEach(v=>{
      if(v.checked){
        totalPrice+=v.num*v.goods_price;
        totalNum+=v.num;
      }else{
        allChecked = false;
      }
    })
    //
    allChecked=cart.length!=0?allChecked:false;
    
    this.setData({
      cart,
      allChecked,
      totalPrice,
      totalNum
    });
     wx.setStorageSync('cart',cart);
  },
  //商品全选
  handleItemAllCheck(){
    //获取data的数据
    let {cart,allChecked}= this.data;
    //修改值
    allChecked = !allChecked;
    //循环修改
    cart.forEach(v=>v.checked=allChecked);
    //填回
    this.setCart(cart);

  },

  async handleItemNumEdit(e){
    const {operation,id} = e.currentTarget.dataset;
    let {cart} =this.data;
    //找到商品索引
    const index = cart.findIndex(v=>v.goods_id===id);
    console.log(index);
    if((cart[index].num+operation)<1){
      console.log("删除");
      const res = await showModel({content:"你是否删除该宝贝？"});
      if(res.confirm){
        cart.splice(index,1);
        this.setCart(cart);
      }
    }else{
       //修改
    cart[index].num+=operation;
    this.setCart(cart);
    }
   
  },

  //提交，结算
  async handlePay(e){
    //地址
    const {address,totalNum} = this.data;
    if(!address.userName){
      await showToast({title:"您还未选择收货地址"});
      return;
    }
    //判断商品
    if(totalNum===0){
      await showToast({title:"你还未选购商品"});
      return;
    }

    wx.navigateTo({
      url: '/pages/pay/index'
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address = wx.getStorageSync('address');
    //获取缓存中购物车的数据,确保类型正确
    const cart  = wx.getStorageSync('cart')||[];
    //1.计算全选,主要有一个返回false就不再循环,判断是否为空
    // const allChecked = cart.length?cart.every(v=>v.checked):false;
   this.setCart(cart);
   this.setData({
     address
   })
  },



})